package org.exez.modules.system.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.exez.common.system.api.ISysBaseAPI;
import org.exez.modules.system.constant.OperateTypeEnum;
import org.exez.modules.system.entity.SysLog;
import org.exez.modules.system.mapper.SysLogMapper;
import org.exez.modules.system.service.ISysLogService;
import org.exez.modules.system.util.VisitorBeanUtils;
import org.exez.modules.system.vo.SysLogVO;
import org.exez.modules.visitor.entity.Institution;
import org.exez.modules.visitor.entity.WhiteList;
import org.exez.modules.visitor.service.IInstitutionService;
import org.exez.modules.visitor.service.IWhiteListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 *
 * @Author zhangweijian
 * @since 2018-12-26
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

	@Resource
	private SysLogMapper sysLogMapper;
	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Autowired
	private IWhiteListService whiteListService;

	@Autowired
	private IInstitutionService institutionService;

	/**
	 * @功能：清空所有日志记录
	 */
	@Override
	public void removeAll() {
		sysLogMapper.removeAll();
	}

	@Override
	public IPage<SysLogVO> listLog(Page<SysLog> page, LambdaQueryWrapper<SysLog> queryWrapper) {
		IPage<SysLogVO> sysLogVOIPage = new Page<>();
		List<SysLogVO> sysLogVOS =new ArrayList();

		IPage<SysLog> sysLogIPage = baseMapper.selectPage(page, queryWrapper);

		List<SysLog> records = sysLogIPage.getRecords();
		BeanUtils.copyProperties(sysLogIPage,sysLogVOIPage);
		if (CollectionUtils.isEmpty(records)) {
			sysLogVOIPage.setRecords(sysLogVOS);
			return sysLogVOIPage;
		}

		sysLogVOS = VisitorBeanUtils.convertList2List(records, SysLogVO.class);

		for (SysLogVO sysLogVO : sysLogVOS) {
			if (sysLogVO.getOperateType().equals(OperateTypeEnum.SAVE.getIndex())){
				sysLogVO.setKeyInformation(OperateTypeEnum.SAVE.getName() + sysLogVO.getKeyInformation());
			}else if (sysLogVO.getOperateType().equals(OperateTypeEnum.IN_EXPORT.getIndex())){
				sysLogVO.setKeyInformation(sysLogVO.getKeyInformation());
			}else {
				if (StringUtils.isBlank(sysLogVO.getKeyInformation())) {
					continue;
				}else {
					String[] split = sysLogVO.getKeyInformation().split(",");

					//查询出详情被操作人信息
					StringBuilder key = new StringBuilder();
					for (String s : split) {
						WhiteList byId = whiteListService.getById(Integer.parseInt(s));
						if (byId != null) {
							key.append(" ").append(byId.getWhiteName());
						}
					}

					sysLogVO.setKeyInformation(OperateTypeEnum.getName(sysLogVO.getOperateType()) + key.toString());
				}
			}

			if (sysLogVO.getLogType().equals(3)){
				sysLogVO.setLogTypeStr("白名单");
			}

			Institution byId = institutionService.getById(sysLogVO.getInstitutionId());
			if (null != byId){
				sysLogVO.setInstitutionName(byId.getInstitutionName());
			}
		}

		sysLogVOIPage.setRecords(sysLogVOS);
		return sysLogVOIPage;
	}

	@Override
	public Long findTotalVisitCount() {
		return sysLogMapper.findTotalVisitCount();
	}

	//update-begin--Author:zhangweijian  Date:20190428 for：传入开始时间，结束时间参数
	@Override
	public Long findTodayVisitCount(Date dayStart, Date dayEnd) {
		return sysLogMapper.findTodayVisitCount(dayStart,dayEnd);
	}

	@Override
	public Long findTodayIp(Date dayStart, Date dayEnd) {
		return sysLogMapper.findTodayIp(dayStart,dayEnd);
	}
	//update-end--Author:zhangweijian  Date:20190428 for：传入开始时间，结束时间参数

	@Override
	public List<Map<String,Object>> findVisitCount(Date dayStart, Date dayEnd) {
		try {
			String dbType = sysBaseAPI.getDatabaseType();
			return sysLogMapper.findVisitCount(dayStart, dayEnd,dbType);
		} catch (SQLException e) {
		}
		return null;
	}
}
